﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewsAggregatorLibrary.DAL;

namespace NewsAggregatorLibrary
{
    public class KeywordsMember : Keywords
    {
        /// <summary>
        /// Retrieves all Keywords from the DB.
        /// </summary>
        /// <returns></returns>
        public static KeywordsMemberCollection RetrieveAll()
        {
            try
            {
                return DB.Select().From(Schema).ExecuteAsCustomCollection<Keywords, KeywordsCollection, KeywordsMember, KeywordsMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Keywords " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a Keywords by ID.
        /// </summary>
        /// <returns></returns>
        public static KeywordsMember RetrieveByID(int id)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.Id).IsEqualTo(id).ExecuteSingle<KeywordsMember>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving a Keywords " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a Keywords by keyword.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static KeywordsMember RetrieveByKeyword(string keyword)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.Keyword).IsEqualTo(keyword).ExecuteSingle<KeywordsMember>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving a Keywords " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a Keywords by keyword.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static KeywordsMemberCollection RetrieveLikeKeyword(string keyword)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.Keyword).ContainsString(keyword).ExecuteAsCustomCollection<Keywords, KeywordsCollection, KeywordsMember, KeywordsMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while retrieving a Keywords " + e.Message, e);
            }
        }

        /// <summary>
        /// Adds a new Keywords to the DB.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static int AddKeyword(string keyword)
        {
            KeywordsMember keywordMember = new KeywordsMember();
            try
            {
                keywordMember.Keyword = keyword;

                keywordMember.Save();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while inserting a new Keywords. " + e.Message);

            }
            return keywordMember.Id;
        }

        /// <summary>
        /// Updates a KeywordsMember.
        /// </summary>
        /// <param name="id">The id of the KeywordsMember to update.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static Boolean UpdateFeed(int id, string keyword)
        {
            Boolean ifUpdate = false;
            try
            {
                KeywordsMember keywordMember = RetrieveByID(id);

                if (keywordMember != null)
                {
                    DB.Update<Keywords>()
                    .Set(Columns.Keyword).EqualTo(keyword)
                    .Where(Columns.Id).IsEqualTo(id).Execute();

                    ifUpdate = true;
                }
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while updating a Keywords with id=" + id + ". " + e.Message);
            }
            return ifUpdate;
        }
    }

    public class KeywordsMemberCollection : KeywordsCollection
    {
    }
}
